package com.ihighest.app.infox.tools;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;

import com.ihighest.app.infox.bean.ImageItem;
import com.ihighest.app.infox.tools.ReadLoadiamgeUrl.ScanCompleteCallBack;

public class ReturnItemImage {
	private Context context;
	private List<ImageItem> images = new ArrayList<ImageItem>();
	public Handler handler;
	
	public ReturnItemImage(Context context, Handler handler) {
		super();
		this.context = context;
		this.handler = handler;
	}

	public void readimage(){
		
		
	    ReadLoadiamgeUrl loadiamgeUrl = new ReadLoadiamgeUrl(context);
	    loadiamgeUrl.scanImages(new ScanCompleteCallBack() {
	
			@Override
			public void scanComplete(Cursor cursor) {
			
				while (cursor.moveToNext()) {
					
					String path = cursor.getString(cursor
							.getColumnIndex(MediaStore.Images.Media.DATA));
					long times = cursor.getLong(cursor
							.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
					
					ImageItem mGridItem = new ImageItem(path, paserTimeToYM(times));
					images.add(mGridItem);

				}
				cursor.close();
				Collections.sort(images, new YMComparator());
				for(int i = 0;i<images.size();i++){
					if(i>50){
						images.remove(i);
					}
				}
				Message message = new Message();
				message.obj = images;
				handler.sendMessage(message);
			}
	    });
	}
	
	private String paserTimeToYM(long time) {
		//����ͬһʱ��
		System.setProperty("user.timezone", "Asia/Shanghai");
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone.setDefault(tz);
		SimpleDateFormat format = new SimpleDateFormat("yyyy��MM��dd��");
		return format.format(new Date(time * 1000L));
	}


}
